package com.studentmanagement.service;

import com.studentmanagement.dao.EnrollmentMapper;
import com.studentmanagement.model.Enrollment;
import com.studentmanagement.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Date;

public class EnrollmentService {
    
    /**
     * 获取所有选课记录
     */
    public List<Enrollment> getAllEnrollments() {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.getAllEnrollments();
        }
    }
    
    /**
     * 根据ID获取选课记录
     */
    public Enrollment getEnrollmentById(Integer id) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.getEnrollmentById(id);
        }
    }
    
    /**
     * 根据学生ID获取选课记录
     */
    public List<Enrollment> getEnrollmentsByStudentId(Integer studentId) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.getEnrollmentsByStudentId(studentId);
        }
    }
    
    /**
     * 根据课程ID获取选课记录
     */
    public List<Enrollment> getEnrollmentsByCourseId(Integer courseId) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.getEnrollmentsByCourseId(courseId);
        }
    }
    
    /**
     * 根据状态获取选课记录
     */
    public List<Enrollment> getEnrollmentsByStatus(String status) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.getEnrollmentsByStatus(status);
        }
    }
    
    /**
     * 搜索选课记录（学生姓名、学号、课程名称或课程代码）
     */
    public List<Enrollment> searchEnrollments(String keyword) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.searchEnrollments("%" + keyword + "%");
        }
    }
    
    /**
     * 添加选课记录
     */
    public boolean insertEnrollment(Enrollment enrollment) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            int rows = mapper.insertEnrollment(enrollment);
            sqlSession.commit();
            return rows > 0;
        }
    }
    
    /**
     * 更新选课记录
     */
    public boolean updateEnrollment(Enrollment enrollment) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            int rows = mapper.updateEnrollment(enrollment);
            sqlSession.commit();
            return rows > 0;
        }
    }
    
    /**
     * 删除选课记录
     */
    public boolean deleteEnrollment(Integer id) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            int rows = mapper.deleteEnrollment(id);
            sqlSession.commit();
            return rows > 0;
        }
    }
    
    /**
     * 检查是否存在相同的选课记录（同一学生选同一门课）
     */
    public boolean checkDuplicateEnrollment(Integer studentId, Integer courseId) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.checkDuplicateEnrollment(studentId, courseId) > 0;
        }
    }
    
    /**
     * 获取学生的成绩统计信息（总学分、平均分等）
     */
    public Enrollment getStudentGradeStats(Integer studentId) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.getStudentGradeStats(studentId);
        }
    }
    
    /**
     * 获取课程的已选学生人数
     */
    public int getEnrolledStudentCount(Integer courseId) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.getEnrolledStudentCount(courseId);
        }
    }
    
    /**
     * 检查学生是否已选修该课程（状态为ACTIVE）
     */
    public boolean isEnrolled(Integer studentId, Integer courseId) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            return mapper.isEnrolled(studentId, courseId) > 0;
        }
    }
    
    /**
     * 学生选课
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 是否选课成功
     */
    public boolean enrollCourse(Integer studentId, Integer courseId) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            
            // 创建新的选课记录
            Enrollment enrollment = new Enrollment();
            enrollment.setStudentId(studentId);
            enrollment.setCourseId(courseId);
            enrollment.setEnrollmentDate(new Date());
            enrollment.setStatus("ACTIVE");  // 设置状态为已选
            
            int rows = mapper.insertEnrollment(enrollment);
            sqlSession.commit();
            return rows > 0;
        }
    }
    
    /**
     * 学生退课
     * @param studentId 学生ID
     * @param courseId 课程ID
     * @return 是否退课成功
     */
    public boolean dropCourse(Integer studentId, Integer courseId) {
        try (SqlSession sqlSession = MyBatisUtil.openSession()) {
            EnrollmentMapper mapper = sqlSession.getMapper(EnrollmentMapper.class);
            
            // 查找当前有效的选课记录
            List<Enrollment> enrollments = mapper.getEnrollmentsByStudentId(studentId);
            for (Enrollment enrollment : enrollments) {
                if (enrollment.getCourseId().equals(courseId) && "ACTIVE".equals(enrollment.getStatus())) {
                    // 更新状态为退选
                    enrollment.setStatus("DROPPED");
                    int rows = mapper.updateEnrollment(enrollment);
                    sqlSession.commit();
                    return rows > 0;
                }
            }
            return false;
        }
    }
} 